package com.demoo.zjsu.util.stringutil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by jshjsh on 2016/8/15.
 */
public class StringUtil {


    /**
     * 总结：
     * substring（0，len）意味着只能切割len-0长度(长度是len-0)
     * lastIndexOf()返回的下坐标.比方说在第10位，就返回9
     * substring(len)从len开始切割到最后
     * @param ip
     * @return
     */
    public static Map<String, String> cutIp(String ip) {
        Map<String, String> map = new HashMap<String, String>();
        int last = ip.lastIndexOf("/");
        String filePath = ip.substring(0, last);
        String fileName = ip.substring(last);
        map.put("filePath", filePath);
        map.put("fileName", fileName);
        return map;
    }

    public static Map<String, String> cutEverything(String ip, char a) {
        Map<String, String> map = new HashMap<String, String>();
        int last = ip.lastIndexOf(a);
        if (last < 0) {
            map.put("0", "");
            map.put("1", "");
            return map;
        }
        String filePath = ip.substring(0, last);
        String fileName = ip.substring(last);
        map.put("0", filePath);
        map.put("1", fileName);
        return map;
    }

    //如果是ip地址是带‘/’符号的

    /**
     * 包括切割的字符
     * @param string
     * @param a
     * @return
     */
    public static String cutLastByChar(String string, char a) {
        int index = string.indexOf(a);
        return string.substring(index);
    }

    /**
     * 不包括切割的字符
     * @param string
     * @param a
     * @return
     */
    public static String cutIpLastByChar(String string, char a) {
        int index = string.indexOf(a);
        return string.substring(index+1);
    }

    /**
     * 切割在字符以前的字符串
     * @param string
     * @param a
     * @return
     */
    public static String cutIpBefore(String string, char a) {
        int index = string.indexOf(a);
        if (index>0)
            return string.substring(0, index);
        else
            return string;
    }

}
